
//获取到的对应登录名的商品信息
		var comm;
		
window.onload=function(){
	//获取到的登录名
		var str=getCookie("userPhone");
		if(str==null){
			str="登录"
		}
		console.log(location.href.substring(location.href.indexOf("?")+1));
		let parma=location.href.substring(location.href.indexOf("?")+1);
		let parmaArr=parma.split("&");
		console.log(parmaArr[0].substring(8))
		console.log(parmaArr[1].substring(6))
		console.log(parmaArr[2].substring(6))
		console.log(parmaArr[3].substring(4))
		console.log(parmaArr)
		// console.log(location.href)
		document.getElementById("login_text").innerHTML=str;
		//获取从上个页面跳转过来的商品信息
        $(".c_title").after("<ul class=\"c_list\">\n" +
            "        <li class=\"checkAll\"><input type=\"checkbox\" class=\"checklist\"/><img src="+parmaArr[0].substring(8)+"></li>\n" +
            "        <li class=\"trade\"><a href=\"decails.html\">"+parmaArr[1].substring(6)+"</a></li>\n" +
            "    <li class=\"unit\">￥<span class=\"price\" id=\"price\">"+parmaArr[2].substring(6)+"</span></li>\n" +
            "    <li class=\"number\">\n" +
            "        <span class=\"jian\" id=\"jian\">-</span>\n" +
            "        <span class=\"num3\" id=\"num3\">"+parmaArr[3].substring(4)+"</span>\n" +
            "        <span class=\"jia\" id=\"jia\">+</span>\n" +
            "        </li>\n" +
            "        <li class=\"subtoal\">￥<span class=\"total\" id=\"total\">23.00</span></li>\n" +
            "    <li class=\"opertator\"><a href=\"#\" class=\"delete\">删除</a></li>\n" +
            "    </ul>);")
	
		// $.ajax({
		// 	type:"get",
		// 	url:"php/getShoppingCart.php",
		// 	async:true,
		// 	data:{
		// 		vipName:str
		// 	},
		// 	success:function(data){
		// 		comm=JSON.parse(data);
		// 		console.log(comm);
		// 		successAfter();
		// 		PriceAndQuantity();
		// 	}
		// });
	}	
function successAfter(){
		for(let i=0;i<comm.length;i++){
					//动态创建所需要的商品个数
					var ulDoms=$("<ul class='c_list'></ul>");
					//ulDoms.addClass("c_list");
					$(".c_title").after(ulDoms);
					var oneli=$("<li></li>");
					oneli.addClass("checkAll");
					ulDoms.append(oneli);
					var inputs=$("<input type='checkbox'/>");
					inputs.addClass("checklist");
					oneli.append(inputs);
					var imgs=$("<img />");
					imgs.attr("src",comm[i].goodsImg);
					//imgs[0].src=comm[i].goodsImg;
					oneli.append(imgs);
					var twoli=$("<li></li>");
					twoli.addClass("trade");
					ulDoms.append(twoli);
					var as=$("<a></a>");
					as.html(comm[i].goodsDesc);
					twoli.append(as);
					var threeli=$("<li></li>");
					threeli.addClass("unit");
					ulDoms.append(threeli);
					var spans=$("<span></span>");
					spans.addClass("price");
					spans.attr("id","price");
					spans.html(comm[i].goodsPrice+".00");
					threeli.append(spans);
					var fourli=$("<li></li>");
					fourli.addClass("number");
					ulDoms.append(fourli);
					var spanone=$("<span></span>");
					spanone.addClass("jian");
					spanone.attr("id","jian");
					spanone.html("-");
					fourli.append(spanone);
					var spantwo=$("<span></span>");
					spantwo.addClass("num3");
					spantwo.attr("id","num3");
					spantwo.html(comm[i].goodsCount);
					fourli.append(spantwo);
					var spanthree=$("<span></span>");
					spanthree.addClass("jia");
					spanthree.attr("id","jia");
					spanthree.html("+");
					fourli.append(spanthree);
					var fiveli=$("<li></li>");
					fiveli.addClass("subtoal");
					ulDoms.append(fiveli);
					spans=$("<span></span>");
					spans.addClass("total");
					spans.attr("id","total");
					spans.html(comm[i].goodsCount*comm[i].goodsPrice+".00");
					fiveli.append(spans);
					var sixli=$("<li></li>");
					sixli.addClass("opertator");
					ulDoms.append(sixli);
					as=$("<a></a>");
					as.addClass("delete");
					as.html("删除");
					sixli.append(as);
				}
	//点击加号
//	document.getElementById("jia").onclick=function(){
//		this.previousElementSibling.innerHTML=parseInt(this.previousElementSibling.innerHTML)+1;
//	}
	
	var jiaArr=document.getElementsByClassName("jia");
	var value=0;
	var sum=0;
	for(let i=0;i<jiaArr.length;i++){
		jiaArr[i].onclick=function(){
			//点击加号改变数据库里的内容
			$.ajax({
			type:"get",
			url:"php/updateGoodsCount.php",
			async:true,
			data:{
				vipName:$("#login_text").html(),
				goodsId:comm[i].goodsId,
				goodsCount:parseInt(this.previousElementSibling.innerHTML)+1
			},
			success:function(data){
					if(data=="1"){
						alert("修改成功");
					}else {					
						alert("修改失败");
					}
			}
		});
//			小计
			let price=this.parentNode.nextElementSibling.children[0].innerHTML;
//			单价
			let unit=this.parentNode.previousElementSibling.children[0].innerHTML;
			this.previousElementSibling.innerHTML=parseFloat(this.previousElementSibling.innerHTML)+1;
			this.parentNode.nextElementSibling.children[0].innerHTML=(parseFloat(this.previousElementSibling.innerHTML)*parseFloat(this.parentNode.previousElementSibling.children[0].innerHTML)).toFixed(2);
			PriceAndQuantity();
		}
	}
	//点击减号
	var jianArr=document.getElementsByClassName("jian");
	for(let i=0;i<jianArr.length;i++){
		jianArr[i].onclick=function(){
			if(this.nextElementSibling.innerHTML<=0){
			this.nextElementSibling.innerHTML=0;
			return;
			}
			this.nextElementSibling.innerHTML=parseInt(this.nextElementSibling.innerHTML)-1;
			this.parentNode.nextElementSibling.children[0].innerHTML=(parseFloat(this.nextElementSibling.innerHTML)*parseFloat(this.parentNode.previousElementSibling.children[0].innerHTML)).toFixed(2);
			PriceAndQuantity();
		}
		
	}
//	删除
	var deleteArr=document.getElementsByClassName("delete");
	for(let j=0;j<deleteArr.length;j++){
		deleteArr[j].onclick=function(){
			if(confirm("亲,您真的要删除吗？")){
			this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
			PriceAndQuantity();
			}
			$.ajax({
			type:"get",
			url:"php/deleteGoods.php",
			async:true,
			data:{
				vipName:$("#login_text").html(),
				goodsId:comm[deleteArr.length-j].goodsId
			},
			success:function(data){
					if(data=="1"){
						alert("删除成功");
					}else {					
						alert("删除失败");
					}
			}
		});
			
			
		}
		
	}
//	删除选中商品
	document.getElementById("trade").onclick=function(){
		var checkList=document.getElementsByClassName("checklist");
		if(confirm("亲,您真的要删除吗？")){
		for(let i=0;i<checkList.length;i++){
			if(checkList[i].checked){
					document.getElementById("content").removeChild(checkList[i].parentNode.parentNode);
			$.ajax({
			type:"get",
			url:"php/deleteGoods.php",
			async:true,
			data:{
				vipName:$("#login_text").html(),
				goodsId:comm[i].goodsId
			},
			success:function(data){
					if(data=="1"){
						alert("删除成功");
					}else {					
						alert("删除失败");
					}
			}
		});
					i=i-1;
				}
	
			}
		
		}
		PriceAndQuantity();
	}
			
}
var numArr=document.getElementsByClassName("num3");
var priceArr=document.getElementsByClassName("total");
function PriceAndQuantity(){
			sum=0;
			for(let i=0;i<numArr.length;i++){
				sum=parseInt(numArr[i].innerHTML)+sum;
			}
			document.getElementById("num4").innerHTML=sum;
			value=0;
			for(let i=0;i<priceArr.length;i++){
				value=parseInt(priceArr[i].innerHTML)+value;
			}
			document.getElementById("money").innerHTML=value.toFixed(2);
}



